package com.csjbot.weedle_auto.fragment

import android.text.method.ScrollingMovementMethod
import android.view.View
import android.widget.Button
import android.widget.TextView
import butterknife.bindView
import com.csjbot.weedle_auto.R
import com.csjbot.weedle_auto.Slam.events.ConnectedEvent
import com.csjbot.weedle_auto.Slam.events.ConnectionLostEvent
import com.csjbot.weedle_auto.Slam.events.RobotStatusUpdateEvent
import com.csjbot.weedle_auto.app.BaseApplication
import com.csjbot.weedle_auto.file.constants_util.Constants
import com.csjbot.weedle_auto.fragment.bean.StationBean
import com.csjbot.weedle_auto.fragment.util.ShareConstants
import com.csjbot.weedle_auto.util.Csjlogger
import com.csjbot.weedle_auto.view.DrawHookView
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by chenqi on 8/4/17.
 */

class AutomaticFramgent : BaseFramgent() {

    val auto: TextView by bindView(R.id.auto)


    private val slamButton: Button by bindView(R.id.slam_button)

    private val draw: DrawHookView by bindView(R.id.draw)


    private var stationBean: StationBean? = null


    //    @Inject
    //    Driver driver;


    override val resureID: Int
        get() = R.layout.automatic_framgent

    override fun initView(view: View) {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this)
        stationBean = StationBean()
        BaseFramgent.sharePreferenceTools.putModel(Constants.SLAMSTATIONBEAN, StationBean())
//        slamButton.setOnClickListener(onClickListener)
        auto.movementMethod = ScrollingMovementMethod.getInstance()
    }

    override fun onStart() {
        super.onStart()

        draw.setOnClickListener {
            draw.requestLayout()
            draw.invalidate()
            Csjlogger.info("chenqi onGlobalLayout")
        }
        //        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        //            @Override
        //            public void onGlobalLayout() {
        //                // TODO Auto-generated method stub
        //                Csjlogger.info("chenqi onGlobalLayout");
        //
        //            }
        //        });
    }

    internal var onClickListener: View.OnClickListener = View.OnClickListener { BaseApplication.instance.agent.connectTo(Constants.SLAM_IP, Constants.SLAM_PORT) }


    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this)
    }

    /**
     * 添加一个
     *
     * @param evet 2017年4月12日 添加了充电显示的功能
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun OnEventMainThread(evet: RobotStatusUpdateEvent) {
        evet.batteryPercentage
        evet.isCharging
        stationBean!!.isCharge = evet.isCharging
        stationBean!!.power = evet.batteryPercentage
        BaseFramgent.sharePreferenceTools.putModel<StationBean>(Constants.SLAMSTATIONBEAN, stationBean)
        refreshView()
    }

    /**
     * 添加一个
     *
     * @param evet 2017年4月12日 添加了充电显示的功能
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun OnEventMainThread(evet: ConnectionLostEvent) {
        stationBean = StationBean()
        BaseFramgent.sharePreferenceTools.putModel(Constants.SLAMSTATIONBEAN, stationBean)
        refreshView()
    }

    /**
     * 添加一个
     *
     * @param evet 2017年4月12日 添加了充电显示的功能
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun OnEventMainThread(evet: ConnectedEvent) {
        evet.msg
        ShareConstants.GetAgentUpdate(BaseApplication.instance.agent)
        stationBean!!.connect = true
        BaseFramgent.sharePreferenceTools.putModel(Constants.SLAMSTATIONBEAN, stationBean)
        //        Csjlogger.info("chenqi automaticFragment{}", stationBean.toString());
        //        ShareConstants.GetAgentUpdate(((BaseApplication)getActivity().getApplication()).getAgent());
    }

    fun refreshView() {
        activity.runOnUiThread {
            auto.hint = ""
            auto.append(stationBean!!.toString())
            val offset = auto.lineCount * auto.lineHeight
            if (offset > auto.height) {
                auto.scrollTo(0, offset - auto.height)
            }
        }
    }

    fun update() {
        if (stationBean!!.connect) {
            stationBean = BaseFramgent.sharePreferenceTools.getModel(Constants.SLAMSTATIONBEAN, StationBean::class.java)
            refreshView()
        }
    }
}
